<?php
/**
 * fatie
 * 
 */
require_once(dirname(__FILE__)."/config.php");
require_once(DEDEINC."/dedetag.class.php");
require_once(DEDEINC."/userlogin.class.php");
require_once(DEDEINC."/customfields.func.php");
require_once(DEDEMEMBER."/inc/inc_catalog_options.php");
require_once(DEDEMEMBER."/inc/inc_archives_functions.php");
$channelid = isset($channelid) && is_numeric($channelid) ? $channelid : 21;
$typeid = isset($typeid) && is_numeric($typeid) ? $typeid : 61;
$mtypesid = isset($mtypesid) && is_numeric($mtypesid) ? $mtypesid : 0;
$menutype = 'content';

$dede_fieldshash = "41353dc9b3cd39b1f0f4ee3b61033ee5";
$dede_addonfields = "";


/*------------------------------
function _SaveArticle(){  }
------------------------------*/
if($dopost=='save')
{
    include(DEDEMEMBER.'/inc/archives_check.php');

   
    $body = AnalyseHtmlBody($content, $description);
    $body = HtmlReplace($body, -1);

    //生成文档ID
    $arcID = GetIndexKey($arcrank, $typeid, $sortrank, $channelid, $senddate, $mid);
    if(empty($arcID))
    {
		$arr = array(
	       "zhi"=>"err",
	        "msg"=>"无法获得主键，因此无法进行后续操作"
        );
        echo json_encode($arr);
        exit;
    }

    //保存到主表
    $inQuery = "INSERT INTO `#@__archives`(id,typeid,sortrank,flag,ismake,channel,arcrank,click,money,title,shorttitle,
color,writer,source,litpic,pubdate,senddate,mid,description,keywords,mtype)
VALUES ('$arcID','$typeid','$sortrank','$flag','$ismake','$channelid','$arcrank','0','$money','$title','$shorttitle',
'$color','$writer','$source','$litpic','$pubdate','$senddate','$mid','$description','$keywords','$mtypesid'); ";
    if(!$dsql->ExecuteNoneQuery($inQuery))
    {
        $gerr = $dsql->GetError();
        $dsql->ExecuteNoneQuery("DELETE FROM `#@__arctiny` WHERE id='$arcID' ");
        
        $arr = array(
	       "zhi"=>"err",
	        "msg"=>"数据提交出错，请联系管理员"
        );
        echo json_encode($arr);
        exit;
    }

    //保存到附加表
    $addtable = trim($cInfos['addtable']);
    if(empty($addtable))
    {
        $dsql->ExecuteNoneQuery("DELETE FROM `#@__archives` WHERE id='$arcID'");
        $dsql->ExecuteNoneQuery("DELETE FROM `#@__arctiny` WHERE id='$arcID'");
       
        $arr = array(
	       "zhi"=>"err",
	        "msg"=>"没找到当前模型[{$channelid}]的主表信息，无法完成操作"
        );
        echo json_encode($arr);
        exit;
    }
    else
    {
        $inquery = "INSERT INTO `{$addtable}`(aid,typeid,userip,redirecturl,templet,body{$inadd_f}) Values('$arcID','$typeid','$userip','','','$body'{$inadd_v})";
        if(!$dsql->ExecuteNoneQuery($inquery))
        {
            $gerr = $dsql->GetError();
            $dsql->ExecuteNoneQuery("DELETE FROM `#@__archives` WHERE id='$arcID'");
            $dsql->ExecuteNoneQuery("DELETE FROM `#@__arctiny` WHERE id='$arcID'");
            $arr = array(
	            "zhi"=>"err",
	            "msg"=>"把数据保存到数据库附加表 `{$addtable}` 时出错，请联系管理员"
            );
            echo json_encode($arr);
            exit;
        }
    }


    //生成HTML
    InsertTags($tags, $arcID);
    $artUrl = MakeArt($arcID, TRUE);
    if($artUrl=='') $artUrl = $cfg_phpurl."/view.php?aid=$arcID";

    
    
    //返回成功信息
    $arr = array(
	            "zhi"=>"ok",
	            "msg"=>"发帖成功，请耐心等待管理员审核！"
    );
    echo json_encode($arr);
    exit;
}